/*
 * Copyright (c) 2024 endless-sky
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <string>
#include <vector>
#include <memory>
#include <ctime>
#include <nlohmann/json.hpp>

namespace El {
namespace Event {

class IStorage {
public:
    static IStorage &GetInstance();

    virtual ~IStorage() = default;

    /**
     * @brief Initialize the storage
     * @param config Configuration in JSON format
     * @return true if successful, false otherwise
     */
    virtual bool init(const nlohmann::json &config) = 0;

    /**
     * @brief Deinitialize the storage and close database connections
     * @return true if successful, false otherwise
     */
    virtual bool deinit() = 0;

    /**
     * @brief Store an event
     * @param event_data JSON formatted event data
     * @return true if successful, false otherwise
     */
    virtual bool store(const nlohmann::json &event_data) = 0;

    /**
     * @brief Query events based on criteria
     * @param start_time Start timestamp (Unix timestamp in milliseconds)
     * @param end_time End timestamp (Unix timestamp in milliseconds)
     * @param event_type Event type filter (empty string for all types)
     * @return Vector of event data in JSON format
     */
    virtual std::vector<nlohmann::json> query(int64_t start_time, int64_t end_time, const std::string &event_type) = 0;

    /**
     * @brief Delete events based on criteria
     * @param start_time Start timestamp (Unix timestamp in milliseconds)
     * @param end_time End timestamp (Unix timestamp in milliseconds)
     * @param event_type Event type filter (empty string for all types)
     * @return Number of events deleted
     */
    virtual int delete_events(int64_t start_time, int64_t end_time, const std::string &event_type) = 0;

    /**
     * @brief Get the total number of stored events
     * @return Total number of events
     */
    virtual size_t count() const = 0;

protected:
    IStorage() = default;
};

} // namespace Event
} // namespace El
